﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Service
{
    public class BalanceTransfer : ICommand
    {
        DataLayer.DBFacade DB;
        private string accountnofrom;
        private int pinfrom;
        private string accountnoto;
        private int pinto;
        private double amount;

        public BalanceTransfer(string accountfrom, int pinfrom, string accountto, int pinto, double amount)
        {
            DB = new DataLayer.DBFacade();
            this.accountnofrom = accountfrom;
            this.pinfrom = pinfrom;
            this.accountnoto = accountto;
            this.pinto = pinto;
            this.amount = amount;
        }

        public bool Execute()
        {
            string query = "SP_Withdraw 'W'," + this.accountnofrom + "," + this.pinfrom + "," + amount;
            bool withdraw= DB.Execute(query) > 0;
            query = "SP_Withdraw 'D'," + this.accountnoto + "," + this.pinto + "," + amount;
            bool deposite = DB.Execute(query) > 0;

            return withdraw && deposite;
        }

        public bool Undo()
        {
            string query = "SP_Withdraw 'D'," + this.accountnofrom + "," + this.pinfrom + "," + amount;
            bool withdraw = DB.Execute(query) > 0;
            query = "SP_Withdraw 'W'," + this.accountnoto + "," + this.pinto + "," + amount;
            bool deposite = DB.Execute(query) > 0;

            return withdraw && deposite;
        }
    }
}